<?php
namespace Home\Controller;
//use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        //$item=D('Admin/Goods')->getView();
        //$this->assign('item',$item);


        //unset($_SESSION);
        //dump($_SESSION);
        $a=M('goods');
        //新品
        $result =$a->join('as a left join '.C("DB_PREFIX").'goods_cats as b on a.goodsCatId1=b.catId')->join('left join '.C("DB_PREFIX").'shops as c on a.shopId=c.shopId')->field('a.*,b.catName,c.shopName')->where('a.goodsFlag=1')->select();
        $m = M('goods_gallerys');
        foreach($result as $key=>$val){
            $result[$key]['gallery'] = $m->where('goodsId='.$val['goodsId'])->limit(4)->select();
        }
        $this->assign('newGoods',$result);
       //dump($result);
         $this->display();
    }

    public function item(){
        $item=D('Admin/Goods')->getView();
        $this->assign('item',$item);
        //dump($item);
         $this->display();
    }

    public function goodsList(){

        //面包导航
        if((int)I('goodsCatId3')>0){
        $r=M('goods_cats')->where('catId='.(int)I('goodsCatId3'))->field('concat(catPath,catId) as bpath,catName')->find();
        $this->assign('catName',$r['catName']);
        $bread=M('goods_cats')->field('catId,catName')->where('catId in ('.$r["bpath"].')')->order('FIND_IN_SET(catId,"'.$r["bpath"].'")')->select();
        $this->assign('bread',$bread);
    }
        //列表页
        $m = D('Goods');
        $PQ = $m->getGoodsPage();
        //dump($PQ);
        $pageSize=(int)I('pageSize',0);
        if($pageSize>0) $PQ['pageSize']=$pageSize;
        $page= getPage($PQ['total'],$PQ['pageSize']);
        $page->setConfig('theme',"<div class='col-md-4 col-sm-4 items-info'>%HEADER%</div><div class='col-md-8 col-sm-8'><ul class='pagination pull-right'><li>%FIRST%</li> <li>%UP_PAGE%</li> <li>%LINK_PAGE%</li> <li>%DOWN_PAGE%</li>
<li>%END%</li></ul></div>");
        $PQ['page'] = $page->show();
        $this->assign('PQ',$PQ);
        $this->assign('shopName',I('shopName'));
        $this->assign('goodsName',I('goodsName'));
        $this->assign('pageSize',$pageSize);
         $this->display('Index/list');
    }

    public function about(){
        //面包导航
        //echo "88888888888";exit;
        $articleId=(int)I('articleId',0);
        if($articleId==0) $this->error('非法访问！');
        
        $m = M('articles');
        $rs= $m->where("articleId=".$articleId)->find();
        $this->assign('articles',$rs);
        $catId=$rs['catId'];
        $rs=M('article_cats')->where('catId='.$catId)->field('parentId,catName')->find();
        $pname=M('article_cats')->where('catId='.$rs['parentId'])->getField('catName');
        $rs['pname']=$pname;
        $this->assign('bread',$rs);
        //dump($rs);
        $this->display();
        
        
        
    }
    public function faq(){
    	$m = M('articles');
	    $faq=$m->where('isShow=1')->order('articleId desc')->select();
		$this->assign('faq',$faq);
        $this->display();
    }
    public function regist(){
         $this->display();
    }

    public function login(){
         $this->display();
    }


// 注册协议

    public function toUserProtocol(){
        $this->display("protocol");
    }

    //找回密码
    public function forget(){
        if(IS_POST){           
            //************************ 配置信息 ****************************
            $smtpserver = $GLOBALS['COMMON']['mailSmtp'];//SMTP服务器
            $smtpserverport =$GLOBALS['COMMON']['mailPort'];//SMTP服务器端口
            $smtpusermail = $GLOBALS['COMMON']['mailAddress'];//SMTP服务器的用户邮箱
            $smtpuser = $GLOBALS['COMMON']['mailUser'];//SMTP服务器的用户帐号
            $smtppass = $GLOBALS['COMMON']['mailPass'];//SMTP服务器的用户密码
            $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
            
            $smtpemailto = I('email');//发送给谁
            $mailtitle = "用户重置密码";//邮件主题
            
            $skey= md5(I('email'));//找回密码skey
            $forgetUrl='http://'.$_SERVER[HTTP_HOST].U('Index/reset').'?skey='.$skey;
            $mailcontent = "<h1>密码重置</h1><hr><p>请点击链接以下前往重置<br><a href='{$forgetUrl}'>{$forgetUrl}</a></p>";//邮件内容
            
            $smtp = new \Tools\Email($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
            $smtp->debug = false;//是否显示发送的调试信息
            $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

            if($state==""){
                //echo "88888888";
                $this->error("对不起，邮件发送失败！请检查邮箱填写是否有误。");
                return;
            }
            $this->success('恭喜您，邮件发送成功！', U('Index/index'));
        }
        $this->display();
    }

    public function reset(){
        echo '';
    }

    public function aboutList(){
        //面包导航
        $catId=I('catId',0);
        if($catId>0){
            $rs=M('article_cats')->where('catId='.$catId)->field('parentId,catName')->find();
            $pname=M('article_cats')->where('catId='.$rs['parentId'])->getField('catName');
            $rs['pname']=$pname;
        }else{
            $rs['catName']="所有文章";
        }
        $this->assign('bread',$rs);

        
        //dump($rs);
        //列表页
        $m = D('Articles');
        $PQ = $m->getArticlesPage();
        //dump($PQ);
        $pageSize=(int)I('pageSize',0);
        if($pageSize>0) $PQ['pageSize']=$pageSize;
        $page= getPage($PQ['total'],$PQ['pageSize']);
        $page->setConfig('theme',"<div class='col-md-4 col-sm-4 items-info'>%HEADER%</div><div class='col-md-8 col-sm-8'><ul class='pagination pull-right'><li>%FIRST%</li> <li>%UP_PAGE%</li> <li>%LINK_PAGE%</li> <li>%DOWN_PAGE%</li>
<li>%END%</li></ul></div>");
        $PQ['page'] = $page->show();
        //dump($PQ);
        $this->assign('PQ',$PQ);
        $this->assign('articleTitle',I('articleTitle'));
        $this->assign('pageSize',$pageSize);
        $this->display();
        
    }
}